﻿using System;
using Farinfo.Infrastructure;

namespace OneWorks.Web.Common.ActionResults
{
    public class AjaxResult<T> : CustomResult<AjaxEntity<T>>
    {
        private readonly RunResult<T> _runResult;

        public AjaxResult(RunResult<T> runResult)
        {
            _runResult = runResult;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override AjaxEntity<T> GetObject()
        {
            AjaxEntity<T> entity = new AjaxEntity<T> { Data = _runResult.Result };
            if (_runResult.Result is bool)
            {
                entity.Success = _runResult.Status == ResultStatus.Success && Convert.ToBoolean(_runResult.Result);
            }
            else
            {
                entity.Success = _runResult.Status == ResultStatus.Success;
            }
            entity.Data = _runResult.Result;
            entity.Message = _runResult.Message;
            return entity;
        }
    }
}
